package com.hung.trade.goods.service.imple;

import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.exception.GoodsException;
import com.hung.trade.common.protocol.goods.*;
import com.hung.trade.dao.mapper.TradeGoodsMapper;
import com.hung.trade.dao.mapper.TradeGoodsNumberLogMapper;
import com.hung.trade.dao.pojo.TradeGoods;
import com.hung.trade.dao.pojo.TradeGoodsNumberLog;
import com.hung.trade.dao.pojo.TradeGoodsNumberLogKey;
import com.hung.trade.goods.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author hungki
 * @createTime 2018-10-09 下午 4:16
 */
@Service
public class GoodsServiceImpl implements IGoodsService {
    private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);
    @Autowired
    private TradeGoodsMapper tradeGoodsMapper;
    @Autowired
    private TradeGoodsNumberLogMapper tradeGoodsNumberLogMapper;
    @Override
    @Transactional(readOnly = true)
    public QueryGoodsResp queryGoodsById(QueryGoodsReq queryGoodsReq) {
        QueryGoodsResp resp = new QueryGoodsResp();


        try{
            if(null==queryGoodsReq || queryGoodsReq.getGoodsId()==null){
                throw new GoodsException("缺少参数goodsId");
            }
            TradeGoods tradeGoods = tradeGoodsMapper.selectByPrimaryKey(queryGoodsReq.getGoodsId());
            if(null==tradeGoods){
                throw new GoodsException(String.format("查询的商品不存在[id = [%s]]",queryGoodsReq.getGoodsId()));
            }
            //拷贝属性到返回值resp里面
            BeanUtils.copyProperties(tradeGoods,resp);
        }catch (Exception e){
            resp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
            throw new GoodsException(String.format("查询商品异常[id = [%s]]",queryGoodsReq.getGoodsId()),e);
        }


        return resp;
    }

    @Override
    @Transactional
    public ReduceGoodsNumberResp reduceGoodsNumber(ReduceGoodsNumberReq reduceGoodsNumberReq) {
        ReduceGoodsNumberResp resp = new ReduceGoodsNumberResp();

        if(null==reduceGoodsNumberReq || reduceGoodsNumberReq.getGoodsId()==null || reduceGoodsNumberReq.getGoodsNumber()<=0){
            throw new GoodsException(String.format("参数不正确 [goodsId = [%s],goodsNumber = [%s]]",reduceGoodsNumberReq.getGoodsId(),reduceGoodsNumberReq.getGoodsNumber()));
        }
        TradeGoods tradeGoods = new TradeGoods();
        tradeGoods.setGoodsId(reduceGoodsNumberReq.getGoodsId());
        tradeGoods.setGoodsNumber(reduceGoodsNumberReq.getGoodsNumber());
        int i  = tradeGoodsMapper.reduceGoodsNumber(tradeGoods);
        if(i<=0){
            throw new GoodsException("扣减库存失败");
        }
        TradeGoodsNumberLog tradeGoodsNumberLog =new TradeGoodsNumberLog();
        tradeGoodsNumberLog.setGoodsNumber(reduceGoodsNumberReq.getGoodsNumber());
        tradeGoodsNumberLog.setGoodsId(reduceGoodsNumberReq.getGoodsId());
        tradeGoodsNumberLog.setOrderId(reduceGoodsNumberReq.getOrderId());
        tradeGoodsNumberLog.setLogTime(new Date());
        i = tradeGoodsNumberLogMapper.insert(tradeGoodsNumberLog);
        if(i<=0){
            logger.error("扣减库存保存日志操作失败");
        }

        //int c= 1/0;
        return resp;
    }

    @Override
    @Transactional
    public AddGoodsNumberResp addGoodsNumber(AddGoodsNumberReq addGoodsNumberReq) {
        AddGoodsNumberResp resp = new AddGoodsNumberResp();

        if(null==addGoodsNumberReq || addGoodsNumberReq.getGoodsId()==null || addGoodsNumberReq.getGoodsNumber()<=0){
            throw new GoodsException(String.format("参数不正确 [goodsId = [%s],goodsNumber = [%s]]",addGoodsNumberReq.getGoodsId(),addGoodsNumberReq.getGoodsNumber()));
        }

        if(addGoodsNumberReq.getOrderId()!=null) {

            TradeGoodsNumberLogKey key = new TradeGoodsNumberLogKey();
            key.setOrderId(addGoodsNumberReq.getOrderId());
            key.setGoodsId(addGoodsNumberReq.getGoodsId());
            TradeGoodsNumberLog log = tradeGoodsNumberLogMapper.selectByPrimaryKey(key);
            if(null==log){
                throw new GoodsException("未找到扣减库存记录");
            }

            TradeGoods tradeGoods = new TradeGoods();
            tradeGoods.setGoodsId(addGoodsNumberReq.getGoodsId());
            tradeGoods.setGoodsNumber(addGoodsNumberReq.getGoodsNumber());
            int i = tradeGoodsMapper.addGoodsNumber(tradeGoods);
            if (i <= 0) {
                throw new GoodsException("增加库存失败");
            }

        }
        return resp;

    }
}
